//
//  COSettingViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/29.
//

import UIKit

class COSettingViewController: COBaseViewController {
    var contents: [COProfileModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        configCell()
    }
    
    override func configUI() {
        self.title = "Setting"
        view.addSubview(tableView)
        view.addSubview(logout)
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(logout.snp.top).offset(0)
            make.top.equalToSuperview().offset(statusBarHeight+navHeight)
        }
        
        logout.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(18)
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(-60)
        }
    }
    
    private func configCell() {
        contents = []
        let feedback = COProfileModel()
        feedback.name = "Feedback"
        feedback.action = {[weak self] in
            let vc = COFeedbackViewController()
            self?.navigationController?.pushViewController(vc, animated: true)
        }
        contents.append(feedback)
        
        let ter = COProfileModel()
        ter.name = "Terms of Service"
        ter.action = {[weak self] in
            let url = KTerms
            let vc = COWebViewController(url)
            self?.navigationController?.pushViewController(vc)
        }
        contents.append(ter)
        
        let pri = COProfileModel()
        pri.name = "Privacy Policy"
        pri.action = {[weak self] in
            let url = KPolicy
            let vc = COWebViewController(url)
            self?.navigationController?.pushViewController(vc)
        }
        contents.append(pri)
        
        let version = COProfileModel()
        version.name = "Version No"
        version.rightHidden = true
        version.content = "V1.0.0"
        contents.append(version)
        
        let del = COProfileModel()
        del.name = "Delete account"
        del.action = {[weak self] in
            let alert = UIAlertController(title: "Are you sure to delete your account", message: "", preferredStyle: .actionSheet)
            let action = UIAlertAction(title: "Confirm", style: .destructive) {[weak alert] action in
                alert?.dismiss(animated: true, completion: {
                    self?.view.makeToastActivity(.center)
                    CONetwork.sendRequest(provider: mineProvider, target: .delete) { _ in
                        self?.view.hideToastActivity()
                        COUserManager.userInfo = nil
                        let vc = COLoginIndexViewController()
                        self?.navigationController?.setViewControllers([vc], animated: true)
                    } failedBlock: { _, text in
                        self?.view.hideToastActivity()
                        self?.view.makeToast(text, position: .center)
                    }
                })
            }
            let cancel = UIAlertAction(title: "Cancel", style: .cancel)
            alert.addAction(action)
            alert.addAction(cancel)
            self?.present(alert, animated: true)
        }
        contents.append(del)
        
        tableView.reloadData()
    }

    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.separatorStyle = .none
        tableV.backgroundColor = .rgba(17, 7, 41, 1)
        tableV.delegate = self
        tableV.dataSource = self
        tableV.register(cellWithClass: COProfileTableViewCell.self)
        return tableV
    }()
    
    private lazy var logout: UIButton = {
        let btn = UIButton()
        btn.setTitle("Log out", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .regular(14)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 25
        btn.backgroundColor = .rgba(41, 32, 63, 1)
        btn.addTarget(self, action: #selector(logoutAction), for: .touchUpInside)
        return btn
    }()
}

extension COSettingViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        57
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: COProfileTableViewCell.self)
        cell.model = contents[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        contents[indexPath.row].action?()
    }
    
    @objc
    private func logoutAction() {
        self.view.makeToastActivity(.center)
        CONetwork.sendRequest(provider: mineProvider, target: .logout) { _ in
            self.view.hideToastActivity()
            COUserManager.userInfo = nil
            let vc = COLoginIndexViewController()
            self.navigationController?.setViewControllers([vc], animated: true)
        } failedBlock: { _, _ in
            self.view.hideToastActivity()
            COUserManager.userInfo = nil
            let vc = COLoginIndexViewController()
            self.navigationController?.setViewControllers([vc], animated: true)
        }

    }
}

